package cn.tedu.tmall.admin.content.service.impl;

import cn.tedu.tmall.admin.content.dao.persist.repository.IContentRepository;
import cn.tedu.tmall.admin.content.pojo.entity.Category;
import cn.tedu.tmall.admin.content.pojo.param.CategoryAddNewParam;
import cn.tedu.tmall.admin.content.pojo.vo.CategoryStandardVO;
import cn.tedu.tmall.admin.content.service.ICategoryService;
import cn.tedu.tmall.common.enumerator.ServiceCode;
import cn.tedu.tmall.common.ex.ServiceException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryService implements ICategoryService {
    @Autowired
    private IContentRepository contentRepository;
    @Override
    public void addNew(CategoryAddNewParam categoryAddNewParam){
        //判断类别名称是否存在
        String name=categoryAddNewParam.getName();
        int count=contentRepository.countByName(name);
        if (count>0){
            //类别名字存在,抛出错误
            throw new RuntimeException("类别名字相同");
        }
        Category category=new Category();
        BeanUtils.copyProperties(categoryAddNewParam, category);
        int rows= contentRepository.insert(category);
        if (rows!=1){
            String message="添加类别失败，服务器忙，请稍后再尝试！";
            throw new ServiceException(ServiceCode.ERROR_INSERT,message);
        }
    }

    @Override
    public CategoryStandardVO getStandardById(Long id) {
        CategoryStandardVO standardById = contentRepository.getStandardById(id);
        if (standardById==null){
            String message="查询类别详情失败，类别数据不存在！";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);

        }
        return standardById;
    }
}
